package org.beanutopia.constraint;

import org.beanutopia.Property;
import org.beanutopia.exception.InvalidValueException;
import com.google.common.base.Predicate;

/**
 * @author ymeymann
 * @since Oct 30, 2007 4:08:25 PM
 */
public class PredicateBasedConstraint<V> implements Constraint<V> {

  private final Predicate<V> predicate;

  public PredicateBasedConstraint(Predicate<V> predicate) {
    this.predicate = predicate;
  }

  @Override public <T extends V> void validate(T value, Property<?, T> property) throws InvalidValueException {
    if (!predicate.apply(value)) throw new InvalidValueException(value, property);
  }
}
